<?php


namespace App\Services;

;

//if (is_file(path_info()['base_path'].'/vendor/phpexcel/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php')){
//    require_once path_info()['base_path'].'/vendor/phpexcel/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
//}else{
//    exit('文件不存在。');
//}

class Excel {

    public function excel_to_array($excel_file) {
        $filename = $excel_file;
        $objPHPExcelReader = \PHPExcel_IOFactory::load($filename);

        $sheet = $objPHPExcelReader->getSheet(0);

        $highestRowIndex = $sheet->getHighestRow();
        $highestColumn = $sheet->getHighestColumn();
        $columnArr = ['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        $highestColumnIndex = array_search($highestColumn,$columnArr);
        print_r($sheet->getCellByColumnAndRow(1,1));exit;

//print_r($sheet->toArray());exit;
        $keyArr = [];
        for ($col=0; $col<$highestColumnIndex; $col++) {
            $keyArr[] = $sheet->getCellByColumnAndRow($col,1)->getValue();
        }

       print_r( $sheet->getCell('A2'));exit;

        print_r($sheet->getCellByColumnAndRow(2,1)->getValue());exit;

        print_r($keyArr);exit;

        for ($row=2; $row<$highestRow; $row++) {

        }

        $val = $sheet->getCellByColumnAndRow(0,1)->getValue();
        echo $val;exit;

        $arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; // 目标列标号
        $row_arr = [];
        for ($row=0;$row<$highestRow;$row++) {

            for ($column = 0; $arr[$column] != 'F'; $column++) {
                $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
                $row_arr[] = $val;
            }

//            $res_arr[] = $row_arr;
        }

        print_r($row_arr);exit;

//        $arr = ['A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; // 目标列标号
//        for ($row = $start_row; $row <= 1000; $row++) { // 一次读取一列
//            $row_arr = [];
//            for ($column = 0; $arr[$column] != 'F'; $column++) {
//                $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
//                echo $val;
//                $row_arr[] = $val;
//            }
//
//            $res_arr[] = $row_arr;
//        }
    }

    // 读取excel
    // 最多只能读10w+1行
    // $excel_file是excel文件的绝对路径；$start_row是起始行，默认2；$end_row是截至行，默认最大行
    public function excel_to_array2($excel_file=null, $start_row = null, $end_row = null){

        $test_data = [
            'excel_file'=> $excel_file,
            'start_row'=> $start_row,
            'end_row'=> $end_row,
        ];

        if (!$excel_file){
            $state = 0;
            $msg = 'Excel路径参数不全';
            $content = [];
        }else{

            //加载excel文件
            $filename = $excel_file;
            $objPHPExcelReader = \PHPExcel_IOFactory::load($filename);

            $sheet = $objPHPExcelReader->getSheet(0); // 读取第一个工作表(编号从 0 开始，sheet)
            if (!$end_row){
                $highestRow = $sheet->getHighestRow(); // 取得最大行数
            }else{
                $highestRow = $end_row;
            }
            $highestColumn = $sheet->getHighestColumn(); // 取得最大列数
            if (!$highestColumn > 100001){
                $state = 0;
                $msg = '最大行数100001行，如有超过，请切割文件';
                $content = [];
            }else{



                if (!$start_row){ // 默认从第二行读
                    $start_row = 2;
                }
                $res_arr = [];
                for ($row = $start_row; $row <= $highestRow; $row++) { // 一次读取一列
                    $row_arr = [];
                    for ($column = 0; $arr[$column] != 'F'; $column++) {
                        $val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
                        echo $val;
                        $row_arr[] = $val;
                    }

                    $res_arr[] = $row_arr;
                }

                $state = 1;
                $msg = 'Excel读取完成';
                $content = $res_arr;

            }

        }

        return [
            'state'=> $state,
            'msg'=> $msg,
            'test_data'=> $test_data,
            'content'=> $content,
        ];

    }


    public function __call($func_name, $args){
        $txt = "class：".__CLASS__." ，函数不存在：$func_name ，参数：$args ";
        exit($txt);
    }

}
